const app = getApp()
var utilBT = require('utilBT.js');
module.exports = {
    deData: deData,
    dataAna: dataAna,
    get_code: get_code,

    toHump: toHump,
    toLine: toLine,
    // strToHexCharCode: strToHexCharCode,
    // hexCharCodeToStr:hexCharCodeToStr,
    StringToHex: StringToHex,
    HexToString: HexToString,
    stringToArrayBuffer: stringToArrayBuffer,
}

var data = []
var t0 = 0

var dedata_res = {
    code: 0,
    ur: {}
}


function arrayHasElement(arr, ele, callback) {  // 判断二维数组array中是否存在一维数组element
    let b = false;
    if (arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].length == ele.length) {
                // app.log(JSON.stringify(arr[i]))
                // app.log(JSON.stringify(ele))
                // app.log(JSON.stringify(ele) == JSON.stringify(arr[i]))
                for (let j = 0; j < arr[i].length; j++) {
                    if (arr[i][j] != ele[j]) {
                        break;
                    }
                    if (i == ele.length - 1) {
                        b = true;
                    }
                }
            }
        }
    }

    callback && typeof callback === 'function' ? callback(b) : null;
}

function deData(res, callback) {
    app.log('==============>>>数据拼包解析')
    let t1 = new Date().getTime()
    app.log('==>>时间差：' + (t1 - t0))
    if (t1 - t0 > 1000) {
        app.log('====>>重置data 1 ')
        data = new Array()
    }
    t0 = t1
    let data0 = new Uint8Array(res.value)
    app.log(data)
    app.log(JSON.stringify(data0))
    for (let i in data0) {
        data.push(data0[i])
    }
    app.log('==>>新数据')
    app.log(data)

    if (data.length < 6) {
        app.log('==>>错误响应')
        return
    }

    switch (data[5]) {
        case 50:
            app.log('==>>getSN')
            dedata_res.code = 50
            dedata_res.ur = {}
            if (data.length > 85 && app.globalData.BLE_SN == 100) {
                app.log('==>>check SN')
                app.log(data)
                let str_sn = ''
                for (let i = 0; i < data.length; i++) {
                    if (data[i] == 42) {
                        app.log('==>>42:' + i)
                        if (str_sn.length < 8) {
                            str_sn += data[i].toString(16).length == 1 ? '0' + data[i].toString(16) : data[i].toString(16)
                            str_sn += data[i + 2].toString(16).length == 1 ? '0' + data[i + 2].toString(16) : data[i + 2].toString(16)
                            str_sn += data[i + 4].toString(16).length == 1 ? '0' + data[i + 4].toString(16) : data[i + 4].toString(16)
                            str_sn += data[i + 6].toString(16).length == 1 ? '0' + data[i + 6].toString(16) : data[i + 6].toString(16)
                            app.log(str_sn)
                            app.log(parseInt(str_sn, 16))

                        }

                    }
                    if (data[i] == 41) {
                        app.log('==>>41:' + i)
                        if (str_sn.length < 8) {
                            str_sn += data[i].toString(16).length == 1 ? '0' + data[i].toString(16) : data[i].toString(16)
                            str_sn += data[i + 1].toString(16).length == 1 ? '0' + data[i + 1].toString(16) : data[i + 1].toString(16)
                            str_sn += data[i + 2].toString(16).length == 1 ? '0' + data[i + 2].toString(16) : data[i + 2].toString(16)
                            str_sn += data[i + 3].toString(16).length == 1 ? '0' + data[i + 3].toString(16) : data[i + 3].toString(16)
                            app.log(str_sn)
                            app.log(parseInt(str_sn, 16))
                        }
                    }

                    if (parseInt(str_sn, 16).toString().indexOf("705") == 0) {
                        app.globalData.BLE_SN = 705
                        app.log('====>>重置data 2 ')
                        data = new Array()
                    } else if (parseInt(str_sn, 16).toString().indexOf("701") == 0) {
                        app.globalData.BLE_SN = 701
                        app.log('====>>重置data 3 ')
                        data = new Array()
                    }
                }
            }

            break;
        case 0:
            app.log('==>>705返回')
            dedata_res.code = 0
            dedata_res.ur = {}

            let number = 0
            for (let i in data){
                number += data[i]
            }

            if (number == 0){
                app.globalData.BLE_SN = 705
                app.log('====>>重置data 4 ')
                data = new Array()
            }

            break;
        case 1:
            app.log('==>>设备确认')
            dedata_res.code = 1
            dedata_res.ur = {}
            checkdata(data, function (res) {
                if (res) {
                    app.log('==>>数据校验成功')
                    if (data[data.length - 1] == 69) {
                        app.log('==>>701 OR 705')
                    } else {
                        let str_sn = ''
                        str_sn += data[6].toString(16).length == 1 ? '0' + data[6].toString(16) : data[6].toString(16)
                        str_sn += data[7].toString(16).length == 1 ? '0' + data[7].toString(16) : data[7].toString(16)
                        str_sn += data[8].toString(16).length == 1 ? '0' + data[8].toString(16) : data[8].toString(16)
                        str_sn += data[9].toString(16).length == 1 ? '0' + data[9].toString(16) : data[9].toString(16)
                        str_sn += data[10].toString(16).length == 1 ? '0' + data[10].toString(16) : data[10].toString(16)

                        app.log(str_sn)
                        app.log(parseInt(str_sn, 16))

                        if (parseInt(str_sn, 16).toString().indexOf("706") == 0) {
                            app.globalData.BLE_SN = 706
                        }else if (parseInt(str_sn, 16).toString().indexOf("705") == 0) {
                            app.globalData.BLE_SN = 705
                        }else if (parseInt(str_sn, 16).toString().indexOf("701") == 0) {
                            app.globalData.BLE_SN = 701
                        }
                    }

                    data = new Array()

                }
            })


            break;
        case 2:
            app.log('==>>时钟同步')
            dedata_res.code = 2
            dedata_res.ur = {}

            break;
        case 4:
            app.log('==>>单条数据')
            dedata_res.code = 4
            dedata_res.ur = {}
            checkdata(data, function (res) {
                if (res) {
                    dataAna(data, function (res) {

                        app.log('==>>dataAna resp suss')
                    })

                    data = new Array()
                }

            })

            break;
        case 5:


            app.log('==>>全部数据')
            dedata_res.code = 5
            dedata_res.ur = {}
            let nn = 0
            let indexs = []
            for (let i = 0; i < data.length; i++) {
                if (data[i] == 147 && data[i + 1] == 142) {
                    // app.log('==>>nn:' + ++nn)
                    // app.log(i)
                    indexs.push(i)
                }
            }

            app.log('==>>indexs[]')
            app.log(indexs)

            if (indexs.length == 0) {

            } else {
                app.log('==>>数据需要分割')

                if (indexs.length == parseInt(data[6].toString(10))) {
                    // app.log('==>>开始拆分数据')

                    let dataarray = []
                    let data00 = []
                    for (let i = 0; i < indexs.length; i++) {
                        if (i == indexs.length - 1) {
                            data00 = data.slice(indexs[i])
                            dataarray.push(data00)
                            // arrayHasElement(dataarray, data00, function (b) {
                            //     if (!b) {
                            //         dataarray.push(data00)
                            //     }
                            // })


                        } else {
                            data00 = data.slice(indexs[i], indexs[i + 1])
                            dataarray.push(data00)
                            // arrayHasElement(dataarray, data00, function (b) {
                            //     if (!b) {
                            //         dataarray.push(data00)
                            //     }
                            // })
                        }

                    }


                    app.log('==>>拆分后的数据')
                    // app.log(dataarray)
                    for (let i in dataarray) {
                        app.log(JSON.stringify(dataarray[i]))
                    }

                    let arr_b = true
                    for (let i in dataarray) {
                        checkdata(dataarray[i], function (res) {
                            app.log('==>>dataarray[' + i + ']检验结果：' + res)
                            if (!res) {
                                arr_b = false;
                            }
                        })
                        if (!arr_b) {
                            break;
                        }
                    }
                    for (let i in dataarray) {
                        if (arr_b) {
                            app.log('==>>数据解析')
                            dataAllMsg(dataarray[i], function (urs) {
                                app.globalData.urs_all = app.globalData.urs_all.concat(urs)
                            })
                        }
                    }
                }
            }

            break;
        case 7:
            dedata_res.code = 7
            dedata_res.ur = {}
            dedata_res.err_msg = '请检查错误'

            app.log('==>>错误代码:' + data[6])

            let err_code = data[6]
            dedata_res.err_code = err_code


            if (app.globalData.BLE_SN == 701) {
                //007=034=021=038=024
                switch (err_code) {
                    case 7:
                        dedata_res.err_msg = '传送仓未达到初始位置'
                        break;
                    case 34:
                        dedata_res.err_msg = '试纸条反放、滑动、未放到位或者未放置试纸条'
                        break;
                    case 38:
                        dedata_res.err_msg = '存储已满需要清理'
                        break;
                }
            } else if (app.globalData.BLE_SN == 705) {
                switch (err_code) {
                    case 7:
                        dedata_res.err_msg = '传送仓未达到初始位置'
                        break;
                    case 8:
                        dedata_res.err_msg = '请检查电机是否卡住，传送仓是否掉落'
                        break;
                    case 10:
                        dedata_res.err_msg = '未放试纸条'
                        break;
                    case 11:
                        dedata_res.err_msg = '试纸条放反'
                        break;
                    case 12:
                        dedata_res.err_msg = '测试过程中试纸条滑动'
                        break;
                    case 38:
                        dedata_res.err_msg = '存储已满需要清理'
                        break;
                    case 39:
                        dedata_res.err_msg = '数据读取错误'
                        break;
                    case 40:
                        dedata_res.err_msg = '数据存储错误'
                        break;
                }
            } else if (app.globalData.BLE_SN == 706) {
                switch (err_code) {
                    case 7:
                        dedata_res.err_msg = '传送仓未达到初始位置'
                        break;
                    case 12:
                        dedata_res.err_msg = '存储已满需要清理'
                        break;
                    case 24:
                        dedata_res.err_msg = '传送仓未达到初始位置'
                        break;
                    case 34:
                        dedata_res.err_msg = '试纸条未放置或者打滑'
                        break;
                }
            }

            break;
        case 11:
            dedata_res.code = 11
            dedata_res.ur = {}
            app.log('==>>测试')
            break;
        case 237:
            dedata_res.code = 237
            dedata_res.ur = {}
            app.log('==>>快速测试')
            break;
        case 238:
            dedata_res.code = 238
            dedata_res.ur = {}
            app.log('==>>取消测试')
            break;
    }

    app.log('==>>BLE_SN:' + app.globalData.BLE_SN)
    if (app.globalData.BLE_SN == 100) {
        setTimeout(function () {
            utilBT.getSn(16)
        }, 1000)
    }

    callback && typeof callback === 'function' ? callback(dedata_res) : null;

}

function checkdata(data, callback) {
    let l = false
    let c = false
    if (data[2] == data.length - 3) {
        app.log('==>>L OK')
        l = true
    }
    let num = 0
    for (let i = 2; i < data.length - 1; i++) {
        num += data[i]
    }
    if (parseInt(num).toString(16).indexOf(parseInt(data[data.length - 1]).toString(16)) > -1) {
        app.log('==>>C OK')
        c = true
    }
    app.log(l)
    app.log(c)

    callback && typeof callback === 'function' ? callback(l && c) : null;
}


function dataAna(data, callback) {
    let num = 0
    switch (data[8] + '' + data[9]) {
        case '7255':
            app.log('==>>11项')
            num = 11
            break;
        case '15255':
            app.log('==>>12项')
            num = 12
            break;
        case '63255':
            app.log('==>>14项')
            num = 14
            break;
    }

    if (num > 0) {
        dataMsg(data, function (res) {
            callback && typeof callback === 'function' ? callback(true) : null;
        })
    }


}

function dataMsg(data, callback) {

    let len = data.length
    let str = ''
    for (let i = 6; i < len - 1; i++) {
        let s = data[i].toString(2)
        let l = 8 - s.length
        for (let j = 0; j < l; j++) {
            s = '0' + s
        }
        str += s
    }

    app.log(data)

    deBin(str, function (ur) {
        dedata_res.ur = ur

    })

    app.log('==>>dedata_res:')
    app.log(dedata_res)

    callback && typeof callback === 'function' ? callback(true) : null;
}

function get_code(str, callback) {
    let code = ''
    let time = utilBT.formatTime3(new Date())
    switch (str) {
        case '0B':
            code = '93 8E 0A 00 08 0B ' + time
            break;
        case '02':
            code = '93 8E 0A 00 08 02 ' + time
            break;
    }
    let codes = code.split(" ")
    let num = 0
    for (let i = 2; i < codes.length; i++) {
        num += parseInt(codes[i], 16)
    }
    let numHex = num.toString(16)
    if (numHex.length > 2) {
        code += ' ' + numHex.substr(numHex.length - 2, 2)
    } else {
        code += ' ' + numHex
    }

    checkCode(code, function (res) {

        if (res) {
            callback && typeof callback === 'function' ? callback(code) : null;
        }
    })

    callback && typeof callback === 'function' ? callback(str) : null;
}

function checkCode(code, callback) {

    app.log(code)
    let l = false
    let c = false
    let num = 0
    let codes = code.split(" ")

    if (parseInt(codes[2], 16) == codes.length - 3) {
        app.log('==>>L true')
        l = true
    }


    for (let i = 2; i < codes.length - 1; i++) {

        // app.log('==【】i='+i+':'+codes[i]+'='+parseInt(codes[i],16))
        num += parseInt(codes[i], 16)
    }

    if (num.toString(16).indexOf(codes[codes.length - 1]) > -1) {
        app.log('==>>C true')
        c = true
    }

    callback && typeof callback === 'function' ? callback(l && c) : null;
}

function dataAllMsg(data, callback) {

    app.log('====>>待解析数据：')

    // app.log(data.length)
    // app.log(data)

    let length = 0
    if (data[8] == (data.length - 10) / 12) {
        app.log('==>>701 数据')
        length = 12
    } else if (data[8] == (data.length - 10) / 14) {
        app.log('==>>705 OR 706 数据')
        length = 14
    }
    let urs = []
    for (let i = 0; i < data[8]; i++) {
        let str_bin = ''
        for (let j = 0; j < length; j++) {
            let s = data[9 + i * length + j].toString(2)
            let l = 8 - s.length
            for (let j = 0; j < l; j++) {
                s = '0' + s
            }
            str_bin += s
        }
        deBin(str_bin, function (ur) {
            urs.push(ur)
        })
    }
    // app.log('====>>urs::')
    // app.log(urs)

    callback && typeof callback === 'function' ? callback(urs) : null;
}

function deBin(str, callback) { //解析二进制数据

    // app.log(str)
    // app.log(str.length)

    let sn = str.substr(6, 10)
    let eff = str.substr(16, 16)
    let day = parseInt(str.substr(32, 5), 2).toString()
    let mon = parseInt(str.substr(37, 4), 2).toString()
    let year = parseInt(str.substr(41, 7), 2).toString()
    let leu = de_result('leu', parseInt(str.substr(50, 3), 2))
    let tm = parseInt(str.substr(53, 6), 2).toString()
    let th = parseInt(str.substr(59, 5), 2).toString()
    let bld = de_result('bld', parseInt(str.substr(65, 3), 2))
    let ph = de_result('ph', parseInt(str.substr(68, 3), 2))
    let pro = de_result('pro', parseInt(str.substr(71, 3), 2))
    let ubg = de_result('ubg', parseInt(str.substr(74, 3), 2))
    let nit = de_result('nit', parseInt(str.substr(77, 3), 2))
    // let pl = de_result('pl', parseInt(str.substr(80, 1), 2))
    let vc = de_result('vc', parseInt(str.substr(81, 3), 2))
    let glu = de_result('glu', parseInt(str.substr(84, 3), 2))
    let bil = de_result('bil', parseInt(str.substr(87, 3), 2))
    let ket = de_result('ket', parseInt(str.substr(90, 3), 2))
    let sg = de_result('sg', parseInt(str.substr(93, 3), 2))

    let cre = de_result('cre', parseInt(str.substr(103, 3), 2))

    let ca = de_result('ca', parseInt(str.substr(106, 3), 2))
    let ma = de_result('ma', parseInt(str.substr(109, 3), 2))


    // app.log('==sn:' + parseInt(sn, 2))
    // app.log('==eff:' + parseInt(eff, 2).toString(16))

    year = year.length == 2 ? year : '0' + year
    mon = mon.length == 2 ? mon : '0' + mon
    day = day.length == 2 ? day : '0' + day
    th = th.length == 2 ? th : '0' + th
    tm = tm.length == 2 ? tm : '0' + tm

    // app.log('==yesr:' + year)
    // app.log('==mon:' + mon)
    // app.log('==day:' + day)
    // app.log('==th:' + th)
    // app.log('==tm:' + tm)
    let time = '20' + year + '-' + mon + '-' + day + ' ' + th + ':' + tm + ':00'
    // app.log('==>>time: ' + time)

    // app.log('==leu:' + leu)
    // app.log('==bld:' + bld)
    // app.log('==ph:' + ph)
    // app.log('==pro:' + pro)
    // app.log('==ubg:' + ubg)
    // app.log('==nit:' + nit)
    // app.log('==pl:' + pl)
    // app.log('==vc:' + vc)
    // app.log('==glu:' + glu)
    // app.log('==bit:' + bit)
    // app.log('==ket:' + ket)
    // app.log('==sg:' + sg)
    // app.log('==cre:' + cre)
    // app.log('==ca:' + ca)
    //
    // app.log('==ma:' + ma)

    let ur = {}
    ur.inspectTime = time
    ur.resultLeu = leu
    ur.resultBld = bld
    ur.resultPh = ph
    ur.resultPro = pro
    ur.resultUbg = ubg
    ur.resultNit = nit
    // ur.pl = pl
    ur.resultVc = vc
    ur.resultGlu = glu
    ur.resultBil = bil
    ur.resultKet = ket
    ur.resultSg = sg
    ur.resultCre = cre
    ur.resultCa = ca
    ur.resultMa = ma
    ur.resultAcr = (ma / cre).toFixed(2)

    // app.log('==>>ur:')
    // app.log(ur)

    callback && typeof callback === 'function' ? callback(ur) : null;

}

function de_result(type, num) {

    let type_num = type.toString().toUpperCase() + '_' + num
    switch (type_num) {

        case 'LEU_0':
            return '阴性(-)'
            break;
        case 'LEU_1':
            return '弱阳性(+-)'
            break;
        case 'LEU_2':
            return '阳性(+1)'
            break;
        case 'LEU_3':
            return '阳性(+2)'
            break;
        case 'LEU_4':
            return '阳性(+3)'
            break;
        case 'LEU_5':
            return '/'
            break;
        case 'LEU_6':
            return '/'
            break;
        case 'LEU_7':
            return '/'
            break;

        case 'NIT_0':
            return '阴性(-)'
            break;
        case 'NIT_1':
            return '阳性(+)'
            break;
        case 'NIT_2':
            return '/'
            break;
        case 'NIT_3':
            return '/'
            break;
        case 'NIT_4':
            return '/'
            break;
        case 'NIT_5':
            return '/'
            break;
        case 'NIT_6':
            return '/'
            break;
        case 'NIT_7':
            return '/'
            break;

        case 'UBG_0':
            return '阴性(-)'
            break;
        case 'UBG_1':
            return '阳性(+1)'
            break;
        case 'UBG_2':
            return '阳性(+2)'
            break;
        case 'UBG_3':
            return '阳性(+3)'
            break;
        case 'UBG_4':
            return '/'
            break;
        case 'UBG_5':
            return '/'
            break;
        case 'UBG_6':
            return '/'
            break;
        case 'UBG_7':
            return '/'
            break;

        case 'PRO_0':
            return '阴性(-)'
            break;
        case 'PRO_1':
            return '弱阳性(+-)'
            break;
        case 'PRO_2':
            return '阳性(+1)'
            break;
        case 'PRO_3':
            return '阳性(+2)'
            break;
        case 'PRO_4':
            return '阳性(+3)'
            break;
        case 'PRO_5':
            return '阳性(+4)'
            break;
        case 'PRO_6':
            return '/'
            break;
        case 'PRO_7':
            return '/'
            break;

        case 'PH_0':
            return '5.0'
            break;
        case 'PH_1':
            return '6.0'
            break;
        case 'PH_2':
            return '6.5'
            break;
        case 'PH_3':
            return '7.0'
            break;
        case 'PH_4':
            return '7.5'
            break;
        case 'PH_5':
            return '8.0'
            break;
        case 'PH_6':
            return '8.5'
            break;
        case 'PH_7':
            return '/'
            break;

        case 'BLD_0':
            return '阴性(-)'
            break;
        case 'BLD_1':
            return '弱阳性(+-)'
            break;
        case 'BLD_2':
            return '阳性(+1)'
            break;
        case 'BLD_3':
            return '阳性(+2)'
            break;
        case 'BLD_4':
            return '阳性(+3)'
            break;
        case 'BLD_5':
            return '/'
            break;
        case 'BLD_6':
            return '/'
            break;
        case 'BLD_7':
            return '/'
            break;

        case 'SG_0':
            return '1.000'
            break;
        case 'SG_1':
            return '1.005'
            break;
        case 'SG_2':
            return '1.010'
            break;
        case 'SG_3':
            return '1.015'
            break;
        case 'SG_4':
            return '1.020'
            break;
        case 'SG_5':
            return '1.025'
            break;
        case 'SG_6':
            return '1.030'
            break;
        case 'SG_7':
            return '/'
            break;

        case 'KET_0':
            return '阴性(-)'
            break;
        case 'KET_1':
            return '弱阳性(+-)'
            break;
        case 'KET_2':
            return '阳性(+1)'
            break;
        case 'KET_3':
            return '阳性(+2)'
            break;
        case 'KET_4':
            return '阳性(+3)'
            break;
        case 'KET_5':
            return '阳性(+4)'
            break;
        case 'KET_6':
            return '/'
            break;
        case 'KET_7':
            return '/'
            break;

        case 'BIL_0':
            return '阴性(-)'
            break;
        case 'BIL_1':
            return '阳性(+1)'
            break;
        case 'BIL_2':
            return '阳性(+2)'
            break;
        case 'BIL_3':
            return '阳性(+3)'
            break;
        case 'BIL_4':
            return '/'
            break;
        case 'BIL_5':
            return '/'
            break;
        case 'BIL_6':
            return '/'
            break;
        case 'BIL_7':
            return '/'
            break;

        case 'GLU_0':
            return '阴性(-)'
            break;
        case 'GLU_1':
            return '弱阳性(+-)'
            break;
        case 'GLU_2':
            return '阳性(+1)'
            break;
        case 'GLU_3':
            return '阳性(+2)'
            break;
        case 'GLU_4':
            return '阳性(+3)'
            break;
        case 'GLU_5':
            return '阳性(+4)'
            break;
        case 'GLU_6':
            return '/'
            break;
        case 'GLU_7':
            return '/'
            break;

        case 'VC_0':
            return '阴性(-)'
            break;
        case 'VC_1':
            return '弱阳性(+-)'
            break;
        case 'VC_2':
            return '阳性(+1)'
            break;
        case 'VC_3':
            return '阳性(+2)'
            break;
        case 'VC_4':
            return '阳性(+3)'
            break;
        case 'VC_5':
            return '/'
            break;
        case 'VC_6':
            return '/'
            break;
        case 'VC_7':
            return '/'
            break;

        case 'MA_0':
            return '0'
            break;
        case 'MA_1':
            return '30'
            break;
        case 'MA_2':
            return '80'
            break;
        case 'MA_3':
            return '150'
            break;
        case 'MA_4':
            return '/'
            break;
        case 'MA_5':
            return '/'
            break;
        case 'MA_6':
            return '/'
            break;
        case 'MA_7':
            return '/'
            break;

        case 'CRE_0':
            return '0.9'
            break;
        case 'CRE_1':
            return '4.4'
            break;
        case 'CRE_2':
            return '17.7'
            break;
        case 'CRE_3':
            return '26.5'
            break;
        case 'CRE_4':
            return '/'
            break;
        case 'CRE_5':
            return '/'
            break;
        case 'CRE_6':
            return '/'
            break;
        case 'CRE_7':
            return '/'
            break;

        case 'CA_0':
            return '0'
            break;
        case 'CA_1':
            return '2.5'
            break;
        case 'CA_2':
            return '5.0'
            break;
        case 'CA_3':
            return '10'
            break;
        case 'CA_4':
            return '/'
            break;
        case 'CA_5':
            return '/'
            break;
        case 'CA_6':
            return '/'
            break;
        case 'CA_7':
            return '/'
            break;


    }
}

// 下划线转换驼峰
function toHump(name) {
    return name.replace(/\_(\w)/g, function (all, letter) {
        return letter.toUpperCase();
    });
}

// 驼峰转换下划线
function toLine(name) {
    return name.replace(/([A-Z])/g, "_$1").toLowerCase();
}

function StringToHex(str) {
    if (str == '')
        return '';
    let hex = [];
    hex.push('0x');
    for (var i = 0; i < str.length; i++) {
        hex.push((str.charCodeAt(i)).toString(16));
    }
    return hex.join('');

}

function HexToString(str) {
    if (str.length % 2 != 0) {
        alter('必须为偶数');
        return '';
    }
    let a;
    let string = [];
    for (var i = 0; i < str.length; i = i + 2) {
        a = parseInt(str.substr(i, 2), 16);
        string.push(String.fromCharCode(a));
    }
    return string.join('');

}


function strToHexCharCode(str) {
    str = str + ''

    if (str === "")
        return "";
    var hexCharCode = [];
    hexCharCode.push("0x");
    for (var i = 0; i < str.length; i++) {
        hexCharCode.push((str.charCodeAt(i)).toString(16));
    }
    return hexCharCode.join("");
}

function hexCharCodeToStr(hexCharCodeStr) {
    var trimedStr = hexCharCodeStr.trim();
    var rawStr =
        trimedStr.substr(0, 2).toLowerCase() === "0x" ?
            trimedStr.substr(2) :
            trimedStr;
    var len = rawStr.length;
    if (len % 2 !== 0) {
        alert("Illegal Format ASCII Code!");
        return "";
    }
    var curCharCode;
    var resultStr = [];
    for (var i = 0; i < len; i = i + 2) {
        curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
        resultStr.push(String.fromCharCode(curCharCode));
    }
    return resultStr.join("");
}

function stringToArrayBuffer(str) {
    var bytes = new Array();
    var len, c;
    len = str.length;
    for (var i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if (c >= 0x010000 && c <= 0x10FFFF) {
            bytes.push(((c >> 18) & 0x07) | 0xF0);
            bytes.push(((c >> 12) & 0x3F) | 0x80);
            bytes.push(((c >> 6) & 0x3F) | 0x80);
            bytes.push((c & 0x3F) | 0x80);
        } else if (c >= 0x000800 && c <= 0x00FFFF) {
            bytes.push(((c >> 12) & 0x0F) | 0xE0);
            bytes.push(((c >> 6) & 0x3F) | 0x80);
            bytes.push((c & 0x3F) | 0x80);
        } else if (c >= 0x000080 && c <= 0x0007FF) {
            bytes.push(((c >> 6) & 0x1F) | 0xC0);
            bytes.push((c & 0x3F) | 0x80);
        } else {
            bytes.push(c & 0xFF);
        }
    }
    var array = new Int8Array(bytes.length);
    for (var i in bytes) {
        array[i] = bytes[i];
    }
    return array.buffer;
}

function arrayBufferToString(arr) {
    if (typeof arr === 'string') {
        return arr;
    }
    var dataview = new DataView(arr.data);
    var ints = new Uint8Array(arr.data.byteLength);
    for (var i = 0; i < ints.length; i++) {
        ints[i] = dataview.getUint8(i);
    }
    arr = ints;
    var str = '',
        _arr = arr;
    for (var i = 0; i < _arr.length; i++) {
        var one = _arr[i].toString(2),
            v = one.match(/^1+?(?=0)/);
        if (v && one.length == 8) {
            var bytesLength = v[0].length;
            var store = _arr[i].toString(2).slice(7 - bytesLength);
            for (var st = 1; st < bytesLength; st++) {
                store += _arr[st + i].toString(2).slice(2);
            }
            str += String.fromCharCode(parseInt(store, 2));
            i += bytesLength - 1;
        } else {
            str += String.fromCharCode(_arr[i]);
        }
    }
    return str;
}